package com.fearless.crm.workbench.web.controller;

import com.fearless.crm.settings.entity.User;
import com.fearless.crm.utils.DateTimeUtil;
import com.fearless.crm.utils.UUIDUtil;
import com.fearless.crm.vo.PaginationVo;
import com.fearless.crm.workbench.entity.Activity;
import com.fearless.crm.workbench.entity.ActivityRemark;
import com.fearless.crm.workbench.service.ActivityService;
import com.sun.org.apache.regexp.internal.RE;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("activity")
public class ActivityController {
    @Resource
    private ActivityService activityService;
//打开市场活动新增页
    @RequestMapping("/add")
    @ResponseBody
    public List<User> getUserList(){
        System.out.println("进到到doActivity处理器方法");
        System.out.println("取得用户信息列表");
        List userList=activityService.getUserListService();
        return userList;
    }
//新增市场活动操作
    @RequestMapping("/save")
    @ResponseBody
    public Map saveActivityController(HttpSession session, Activity activity){
        Map map=new HashMap();
        System.out.println("进入到saveActivityController处理器方法");

        String createBy=((User)session.getAttribute("user")).getName();
        activity.setCreateBy(createBy);
        String id= UUIDUtil.getUUID();
        String createTime=DateTimeUtil.getSysTime();
        activity.setCreateTime(createTime);
        activity.setId(id);
        map=activityService.saveActivityService(activity);
        return map;
    }
//分页查询
    @RequestMapping("/pageList")
    @ResponseBody
    public PaginationVo pageListController(Activity activity,Integer pageNo,Integer pageSize){
        System.out.println("进入到pageListController处理器方法");
        Map map=new HashMap();
        String name=activity.getName();
        String owner=activity.getOwner();
        String startDate=activity.getStartDate();
        String endDate=activity.getEndDate();

        //每页展现的纪录数:pageSize

        //计算出略过的记录数
        int skipCount=(pageNo-1)*pageSize;
        map.put("name",name);
        map.put("owner",owner);
        map.put("startDate",startDate);
        map.put("endDate",endDate);
        map.put("pageSize",pageSize);
        map.put("skipCount",skipCount);

        PaginationVo<Activity> paginationVo= activityService.pageListService(map);
        //分页查询频率高，所有我们使用vo来给前端展现数据。如果复用性低，就用map
        //vo是后台给前端传数据的。不是前台给后台传数据的



        return paginationVo;
    }


//市场活动删除操作
    @RequestMapping(value = "/delete" )
    @ResponseBody
    public Map<String,Object> deleteController(@RequestParam(value = "id" ) String[] ids){
        System.out.println("进入到deleteController处理器方法");
        System.out.println("执行市场活动的删除操作");
        /*System.out.println("接收到的参数1"+ids[0]);
        System.out.println("接收到的参数2"+ids[1]);*/
        Boolean flag=activityService.deleteService(ids);
        Map map=new HashMap();
        map.put("success",flag);
        return map;
    }

//打开市场活动修改页
    @RequestMapping("/edit.do")
    @ResponseBody
    public Map<String,Object> editController(String id){
        System.out.println("进入到editController处理器方法中");
        System.out.println("打开修改市场活动模态窗口");
        Map<String,Object> map=activityService.getUserListAndActivity(id);
        return map;
    }

//执行修改市场活动操作
    @RequestMapping("/update.do")
    @ResponseBody
    public boolean updateController(HttpSession session,Activity activity){
        System.out.println("进入到updateController处理器方法");
        System.out.println("执行修改操作");
        //修改时间
        String editTime=DateTimeUtil.getSysTime();

        //修改人:登录用户
        String editBy=((User)session.getAttribute("user")).getName();
        activity.setEditTime(editTime);
        activity.setEditBy(editBy);

        System.out.println(activity.toString());
        Boolean flag=activityService.updateService(activity);
        return flag;
    }


//详细信息页面
    @RequestMapping("/detail.do")
    @ResponseBody
    public ModelAndView detailController(HttpServletRequest request, String id){
        ModelAndView mv=new ModelAndView();
        System.out.println("进入到detailController处理器方法");

        Activity activity=activityService.detailService(id);
        System.out.println(activity.toString());
        request.setAttribute("activity",activity);
        mv.setViewName("forward:/workbench/activity/detail.jsp");
        return mv;
    }


//展现备注信息
    @RequestMapping("/viewRemark.do")
    @ResponseBody
    public List<ActivityRemark> viewRemarkController(String activityId){
        System.out.println("进入到viewRemarkController处理器方法");
        System.out.println("展现备注信息");
        System.out.println("接收到的id参数："+activityId);
        List<ActivityRemark> remarkList=activityService.getRemarkListByAid(activityId);
        return remarkList;
    }

//删除备注信息
    @RequestMapping("/deleteRemarkById.do")
    @ResponseBody
    public boolean deleteRemarkByIdController(String id){
        boolean flag=activityService.deleteRemarkByIdService(id);
        return flag;
    }

//新增备注信息
    @RequestMapping("/addRemark.do")
    @ResponseBody
    public Map<String,Object> addRemarkController(HttpSession session,ActivityRemark activityRemark){
        System.out.println("进入到addRemarkController处理器方法");
        System.out.println("执行备注信息添加操作");
        System.out.println("接收到的ActivityId"+activityRemark.getActivityId());
        System.out.println("接收到的noteContent"+activityRemark.getNoteContent());
        String id=UUIDUtil.getUUID();
        String createTime=DateTimeUtil.getSysTime();
        String createBy=((User)session.getAttribute("user")).getName();
        String editFlag="0";
        activityRemark.setEditFlag(editFlag);
        activityRemark.setId(id);
        activityRemark.setCreateBy(createBy);
        activityRemark.setCreateTime(createTime);

        boolean flag=activityService.addRemarkService(activityRemark);
        Map map=new HashMap();
        map.put("success",flag);
        map.put("ar",activityRemark);

        return map;
    }

//编辑备注信息,打开修改备注的模态窗口
    @RequestMapping("/editRemark.do")
    @ResponseBody
    public Map<String,Object> editRemarkController(String id){
        System.out.println("进入到editRemarkController处理器方法中");
        System.out.println("执行备注信息的修改操作");
        Map map=activityService.editRemarkService(id);
        return map;
    }

//编辑备注信息，执行修改备注的操作
    @RequestMapping("/updateRemark.do")
    @ResponseBody
    public Map<String,Object> updateRemarkController(HttpSession session,ActivityRemark activityRemark){
        boolean flag=activityService.updateRemarkService(activityRemark);
        String editTime=DateTimeUtil.getSysTime();
        String editBy=((User)session.getAttribute("user")).getName();
        activityRemark.setEditTime(editTime);
        activityRemark.setEditBy(editBy);
        Map map=new HashMap();
        map.put("success",flag);
        map.put("activityRemark",activityRemark);
        return map;
    }


//在详细信息页中打开修改市场活动信息的模态窗口
    @RequestMapping("/editActivityInDetail.do")
    @ResponseBody
    public Map<String,Object> editActivityInDetailController(String id){
        System.out.println("进入到editActivityInDetailController处理器方法");
        System.out.println("用于打开修改市场活动的模态窗口");
        System.out.println("接收到的市场活动信息表的id"+id);

        return activityService.getUserListAndActivityInDetailService(id);
    }

//在详细信息页中执行修改市场活动信息的操作
    @RequestMapping("/updateActivityInDetail.do")
    @ResponseBody
    public boolean editActivityModalController(HttpSession session,Activity activity){
        System.out.println("进入到editActivityModalController处理器方法");
        System.out.println("执行在详细信息页中修改市场活动信息的操作");

        String editTime=DateTimeUtil.getSysTime();
        String editBy=((User)session.getAttribute("user")).getName();

        activity.setEditBy(editBy);
        activity.setEditTime(editTime);



        return activityService.updateService(activity);
    }

}
